<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/5
 * Time: 9:43
 */

namespace app\sports_admin\controller;

use app\exception\ContentsException;
use app\sports_admin\model\Region;
use app\sports_admin\model\TableBar;
use app\sports_admin\model\TableChapter;
use think\Request;
use app\sports_admin\service\ExamVideo as ExamVideoService;
use app\sports_admin\model\TableVideo;
use app\sports_admin\model\TableTeacher;
use think\Exception;
use app\sports_admin\model\Cat;
use app\exception\ExamVideoException;

class ExamVideo extends Base
{
    protected $beforeActionList = [
        'isResource' => ['only'=>'index,examvideos,section,videolist,gather,updategather,photo,video,upsection,updatesection,
                        addsection,addsectionmessage,addsectionvideo,addbarvideo,delbarvideo,delsection,addgatherpage,addgather,delgather'],
        'operationSelect'=>['only'=>'index,examvideos,section,videolist,gather,upsection,addsection,addsectionvideo,addgatherpage'],
        'operationDelete'=>['only'=>'delbarvideo,delsection,delgather']
    ];
    //查看中考视频合集的页面
    public function index()
    {
        $cat    = Cat::field("cat_id as id,cat_name")->select();
        return view('list',compact("cat"));
    }
    //返回中考视频的集合
    public function examVideos()
    {
        //$message = CustomerService::showList();
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','type','searchType']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $searchType = trim($params['searchType']);
        $result = TableVideo::compilationVideo($page,$rows,$sort,$sortOrder,$searchName,$searchType);
        return json($result);
    }
    //章节信息
    public function section()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        $result  = ExamVideoService::section($id);

        return view('section',compact('result','message'));
    }
    //章节下的视频
    public function videoList()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        $result  = ExamVideoService::videoList($id);
        return view('video',compact('result','message'));
    }
    //编辑合集信息
    public function gather()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        $result  = TableVideo::gather($id);
        //$img传递的是图片的相对路径
        $model = new TableVideo();
        $teacher = TableTeacher::teacherMessage();
        $city    = Region::addGatherPage();
        return view('list_update',compact('result','teacher','city'));
    }
    //更新章节信息
    public function updateGather(Request $request)
    {
        if($request->isAjax()){
            $result = ExamVideoService::updateGather();
            if($result){
                return json(['code'=>201,'msg'=>'更新成功']);
            }else{
                throw new Exception('服务器内部错误');
            }
        }
    }
    //上传图片
    public function photo()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH.'public/uploads/exam_video/img');
            if($info){
                return json(['img'=>$info->getSaveName()]);
            }else{
                // 上传失败获取错误信息
                return json(['error'=>$file->getError()]);
            }
        }
    }
    //上传视频
    public function video()
    {
        // 获取表单上传文件
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH.'public/uploads/exam_video/video');
            if($info){
                return json(['video'=>$info->getSaveName()]);
            }else{
                // 上传失败获取错误信息
                return json(['error'=>$file->getError()]);
            }
        }
    }

    //编辑章节信息
    public function upSection()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        //查询到信息后，跳转页面
        $val     = TableBar::bar($id);

        return view('bar',compact('val'));
    }
    //更新视频的信息
    public function updateSection(Request $request)
    {
        if($request->isAjax()){
            $res = TableBar::updateSection($request);
            if ($res){
                return json(['msg'=>'更新成功','code'=>200]);
            }else{
                throw new ExamVideoException([
                    'msg'=>'更新失败',
                    'errorCode'=>400
                ]);
            }
        }
    }
    //添加章节
    public function addSection()
    {
        $data = Request::instance()->only('id');
        return view('add_section',compact('data'));
    }
    //添加章节信息
    public function addSectionMessage(Request $request)
    {
        if($request->isAjax()){
            $data = TableChapter::addSectionMessage();
            if ($data){
                return json(['msg'=>'添加成功','code'=>200]);
            }else{
                throw new ContentsException([
                    'msg'=>'内容信息添加失败',
                    'errorCode'=>400]);
            }
        }
    }
    //添加子级是视频的页面
    public function addSectionVideo()
    {
        $message = Request::instance()->only('id');
        return view('add_video',compact('message'));
    }
    //添加子级视频
    public function addBarVideo(Request $request)
    {
        if($request->isAjax()){
            $data = TableBar::addVideo();
            if ($data){
                return json(['msg'=>'添加成功','code'=>200]);
            }else{
                throw new ContentsException([
                    'msg'=>'内容信息添加失败',
                    'errorCode'=>400]);
            }
        }
    }
    //删除子级视频
    public function delBarVideo(Request $request)
    {
        if($request->isAjax()){
            $data = TableBar::delBarVideo($request);
            if ($data){
                return json(['msg'=>'删除成功','code'=>200]);
            }else{
                throw new ContentsException([
                    'msg'=>'删除失败',
                    'errorCode'=>400]);
            }
        }
    }
    //删除章节信息
    public function delSection(Request $request)
    {
        if($request->isAjax()){
            $data = TableChapter::delSection($request);
            if ($data){
                return json(['msg'=>'删除成功','code'=>200]);
            }else{
                throw new ContentsException([
                    'msg'=>'删除失败',
                    'errorCode'=>400]);
            }
        }
    }
    //添加集合页面
    public function addGatherPage()
    {
        $teacher = TableTeacher::teacherMessage();
        $city    = Region::addGatherPage();
        $cat     = Cat::select();
        return view('add_gather',compact('teacher','city','cat'));
    }
    //添加集合
    public function addGather(Request $request)
    {
        if($request->isAjax()){
            $data = TableVideo::addGather();
            if ($data){
                return json(['msg'=>'添加成功','code'=>200]);
            }else{
                throw new ContentsException([
                    'msg'=>'添加失败',
                    'errorCode'=>400]);
            }
        }
    }
    //删除集合
    public function delGather(Request $request)
    {
        if($request->isAjax()){
            $data = TableVideo::delGather($request);
            if($data){
                return json(['msg'=>'删除成功','code'=>200]);
            }else{
                throw new ContentsException([
                    'msg'=>'删除失败',
                    'errorCode'=>400]);
            }
        }
    }

    public function test()
    {
       try{
            return 1;
       }catch (Exception $e){
            return json($e->getMessage());
       }
    }
}